<?php
/**
 * Created by PhpStorm.
 * User: sunku
 * Date: 2018/2/27
 * Time: 9:39
 */

namespace Resource\Model;
use Common\ORG\Util\AuthTable;
use Think\Model;


class UserDepartmentModel extends Model
{
    protected $trueTableName = AuthTable::TB_AUTH_USER_DEPARTMENT;

    /**
     * 查询用户关联的部门
     * @param $user_id
     * @return mixed
     */
    public function getUserRelationDepartmentList($user_id) {
        $tb_auth_user_department = AuthTable::TB_AUTH_USER_DEPARTMENT;
        $tb_auth_department = AuthTable::TB_AUTH_DEPARTMENT;

        $userDepartmentList = M("{$tb_auth_user_department} haua")
            ->join("{$tb_auth_department} ha ON ha.id=haua.department_id")
            ->where("uid='{$user_id}'")
            ->getField('haua.department_id, haua.uid, ha.name', true);
        return $userDepartmentList;
    }

    /**
     * 删除用户关联的部门
     * @param $user_id
     * @return bool
     */
    public function deleteUserRelationDepartment($user_id) {
        $result = $this->where("uid='{$user_id}'")->delete();
        if($result !== false) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 添加用户关联的部门
     * @param $params
     * @return mixed
     */
    public function addUserRelationDepartment($params) {
        return $this->add($params);
    }

    /**
     * 查询某个部门下的用户id列表
     * @param $department_id
     * @return mixed
     */
    public function getDepartmentUserIdList($department_id) {
        $uidList = $this->where("department_id='{$department_id}'")->getField('uid', true);
        return $uidList;
    }

}